import h5py
import numpy as np


def read_matlab_struct(file_path, struct_name):
    with h5py.File(file_path, 'r') as f:
        struct_group = f[struct_name]

        # Initialize lists to store the field values
        X_values = []
        Y_values = []

        # Get the number of structures
        num_structs = struct_group['X'].shape[0]

        # Iterate over each structure
        for i in range(num_structs):
            # Access the 'X' and 'Y' fields for the i-th structure
            X_data_ref = struct_group['X'][i, 0]
            Y_data_ref = struct_group['Y'][i, 0]

            # Each field is a reference to a dataset, load the data
            X_data = np.array(f[X_data_ref]).flatten()
            Y_data = np.array(f[Y_data_ref]).flatten()

            X_values.append(X_data)
            Y_values.append(Y_data)

        # Convert lists to numpy arrays and transpose
        X_values = np.array(X_values).T
        Y_values = np.array(Y_values).T

        return X_values, Y_values
